package com.angel.web.app;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.JsonUtil;
import com.angel.health.BuzAssessment;
import com.angel.health.BuzSurvey;
import com.angel.provider.health.BuzAssessmentProvider;
import com.angel.provider.health.BuzSurveyProvider;

@Controller
@RequestMapping("app/survey")
public class AppSurveyController {

	@Autowired
	private BuzSurveyProvider buzSurveyProvider;
	@Autowired
	private BuzAssessmentProvider buzAssessmentProvider;

	/**
	 * 问卷调查
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveOrUpadte")
	public String saveOrUpadte(BuzSurvey buzSurvey) {
		BuzSurvey b = buzSurveyProvider.findByCustomerId((buzSurvey.getCustomerId()).toString());
		if (b != null) {
			buzSurveyProvider.updateSurvey(buzSurvey);
			// BuzAssessment search = new BuzAssessment();
			// search.setCustomerId(buzSurvey.getCustomerId());
			// List<Map<String, Object>> list =
			// buzAssessmentProvider.findAssessmentAll(search);
			// if(list!=null){
			// if(list.size()==0){
			// BuzAssessment ba = new BuzAssessment();
			// ba.setCustomerId(buzSurvey.getCustomerId());
			// ba.setSurveyId(buzSurvey.getId());
			// ba.setEnable(1);
			// buzAssessmentProvider.insert(ba);
			// ba.setCode(CodeUtil.createCode(null, Constants.ASSESSMENT,
			// ba.getId()));
			// buzAssessmentProvider.updateById(ba);
			// }
			// }
		} else {
			buzSurveyProvider.save(buzSurvey);
			/**
			 * 添加慢性评估》问卷调查
			 */
			BuzAssessment search = new BuzAssessment();
			search.setCustomerId(buzSurvey.getCustomerId());
			List<Map<String, Object>> list = buzAssessmentProvider.findAssessmentAll(search);
			BuzAssessment ba = new BuzAssessment();
			ba.setCustomerId(buzSurvey.getCustomerId());
			ba.setSurveyId(buzSurvey.getId());
			if (list.size() == 0) {
				ba.setEnable(1);
				buzAssessmentProvider.insert(ba);
				ba.setCode(CodeUtil.createCode(null, Constants.ASSESSMENT, ba.getId()));
				buzAssessmentProvider.updateById(ba);
			}else{
				String id = list.get(0).get("id").toString();
				ba.setId(Integer.parseInt(id));
				buzAssessmentProvider.updateById(ba);
			}
		}
		return JsonUtil.toJson(new JsonEntity("200", buzSurvey));
	}

	/**
	 * 问卷update
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loadData/{customerId}", produces = "application/json; charset=UTF-8")
	public String udpate(@PathVariable("customerId") String customerId) {
		BuzSurvey b = buzSurveyProvider.findByCustomerId(customerId);
		return JsonUtil.toJson(new JsonEntity("200", b == null ? "null" : b));
	}
}
